package com.dnake.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dnake.Constant;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.BuildConfig;
import com.dnake.panel.R;
import com.dnake.utils.AppUtils;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class AdminFragment extends SupportFragment {
    @Bind(R.id.tv_password)
    TextView tvPassword;
    @Bind(R.id.iv_erweima)
    ImageView ivErweima;
    @Bind(R.id.tv_tip)
    TextView tvTip;
    @Bind(R.id.tv_time_test)
    TextView tvTimeTest;
    private View mView;

    public static AdminFragment newInstance() {
        return new AdminFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_admin, container, false);
        ButterKnife.bind(this, mView);
        createQRCodeWithLogo();
        if (BuildConfig.DEBUG) {
            tvTip.setText("设备二维码\nV " + BuildConfig.VERSION_NAME + "(" + BuildConfig.VERSION_CODE + ")" + "\nDebug");
        } else {
            tvTip.setText("设备二维码\nV " + BuildConfig.VERSION_NAME + "(" + BuildConfig.VERSION_CODE + ")" + "\nRelease");
        }

        String timeTest = SharedPreferencesUtils.getString("timeTest");
        tvTimeTest.setText(timeTest);
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    /**
     * 生成二维码
     */
    private void createQRCodeWithLogo() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap logoBitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_logo);
                return QRCodeEncoder.syncEncodeQRCode(AppUtils.getMacAddress(), BGAQRCodeUtil.dp2px(getActivity(), 150), Color.BLACK, Color.WHITE, logoBitmap);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivErweima.setImageBitmap(bitmap);
                }
            }
        }.execute();
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_ADMIN) {
            tvPassword.setText(eventBusBean.getName());
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
